import java.awt.*;
import java.awt.image.*;

public class EditorPanel extends Panel implements Runnable
{
   MainFrame mf = null;
   BufferedImage iBuf = null;
   Graphics gBuf = null;
   Thread t = null;
   boolean running = false;

   public EditorPanel(MainFrame mainframe)
   {
      super();
      mf = mainframe;
      iBuf = new BufferedImage(mf.area_width,mf.area_height,BufferedImage.TYPE_INT_RGB);
      gBuf = iBuf.getGraphics();
      t = new Thread(this,"Editor");
   }
   void start()
   {
   		running = true;
   		t.start();
   }
   void stop()
   {
   		running = false;
   }

	Image img;
	int i,j,w,h;
	public void redraw()
	{
		Graphics g = getGraphics();
		if (g != null)
		{
			int aw = mf.area_width;
			int ah = mf.area_height;

			gBuf.setClip(0,0,aw,ah);

			if (mf.bg == null)
			{
				gBuf.setColor(mf.areaColor);
				gBuf.fillRect(0,0,aw,ah);
			}
			else
			{
				img = mf.bg.image;
				w = img.getWidth(null);
				h = img.getHeight(null);
   	
				for (i = 0; i < aw; i += w)
					for (j = 0; j < ah; j += h)
						gBuf.drawImage(img,i,j,null);
			}
			gBuf.setColor(new Color(60,60,60));
			if (mf.cbGrid.getState())
			{
				for (i = 0; i < aw; i += 10) gBuf.drawLine(i,0,i,ah);
				for (i = 0; i < ah; i += 10) gBuf.drawLine(0,i,aw,i);
			}

			mf.draw(gBuf);

			g.setClip(0,0,aw,ah);
			g.drawImage(iBuf,0,0,null);
		}
	}

   public void paint(Graphics g)
   {
      redraw();
   }

   public void run()
   {
   		while (running)
   		{
   			redraw();
   			try
   			{
   				t.sleep(50);
   			}
   			catch(Exception ex){}
   		}
   }



}